import psycopg2
import json
def getHistory():
   conn = psycopg2.connect("dbname=airtime user=airtime password=airtime host=localhost")
   cur = conn.cursor()
   cur.execute("select track_title, artist_name from cc_files,  (select * from (SELECT     playlist_id, starts FROM cc_schedule WHERE ((localtimestamp - interval '30 day'), localtimestamp) OVERLAPS (starts, ends)) as cc_pl_history , cc_playlistcontents where cc_pl_history.playlist_id = id) as cc_history where cc_files.id = cc_history.file_id ORDER BY starts ASC")
   #historyData = cur.fetchall()
   rows = [x for x in cur]
   cols = [x[0] for x in cur.description]
   songs = []
   for row in rows:
      song = {}
      for prop, val in zip(cols, row):
         song[prop] = val
      songs.append(song)
   #print songs
   songsJSON = json.dumps(songs)
   print songsJSON
   cur.close()
   conn.close()

getHistory()
